<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PollPortlet.php 409 2011-01-04 21:21:04Z steen.rabol $
 * @package Pradoportal.Porttlets.PollPortlet
 *
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Portlets.PollPortlet.Common.PollPortletRecord');

class PollPortlet extends Portlet
{
	public function getPoll()
	{
		return $this->ConfigRecord;
	}

	public function setPoll($value)
	{
		$this->ConfigRecord	= $value;
	}

	public function onInit($param)
	{
		parent::onInit($param);

		$this->Poll	= PollPortletRecord::finder()->findBycontentid($this->Parameters['id']);
		if($this->Poll)
		{
			$this->CastVote->Id					= "CastVote_" . $this->Poll->id;
			$this->CastVote->ValidationGroup	= "valPoll_" . $this->Poll->id;
			$this->ChoiceRb->ValidationGroup	= "valPoll_" . $this->Poll->id;
			$this->valChoice->ValidationGroup	= "valPoll_" . $this->Poll->id;
		}
		else
		{
			$this->Poll	= new PollPortletRecord();
		}
	}

	public function OnLoad($parm)
	{
		parent::OnLoad($parm);
		if($this->Poll !== null)
		{
			$cookie = PortalUtil::getCookie("pp_poll_" . $this->Poll->id);

			if(($cookie === null) && ($this->Poll->active == 1 || $this->Poll->active == true) && $this->Poll->expire > time())
			{
				$this->pnlShowPollChoices->Visible	= true;
				$this->pnlShowPollChoices->Enabled	= true;
				$this->pnlPollResult->Visible		= false;
				$this->pnlPollResult->Enabled		= false;

				$answers	= $this->DataAccess->createCommand("select * from tblpollchoices where pollid='" . $this->Poll->id . "' order by displayorder")->query();
				$this->ChoiceRb->DataSource			= $answers;
				$this->ChoiceRb->dataBind();
			}
			else
			{
				$this->pnlShowPollChoices->Visible	= false;
				$this->pnlShowPollChoices->Enabled	= false;
				$this->pnlPollResult->Visible		= true;
				$this->pnlPollResult->Enabled		= true;
				$this->DrawResult();
			}
		}
	}

	public function CastVote($sender,$param)
	{
		if($this->Page->IsPostBack && $this->Page->IsValid)
		{
			$indices	= $this->ChoiceRb->SelectedIndices;
			$index		= $indices[0];
			$item		= $this->ChoiceRb->Items[$index];
			$this->DataAccess->createCommand("update tblpollchoices set votes=votes+1 where pollid=" . $this->Poll->id . " && id=" . $item->Value)->execute();
			$cookie = new THttpCookie("pp_poll_" . $this->Poll->id,"1");
			$cookie->Expire	= time() + (60 * 60 * 24 * 365); // 1 year;
			$this->Response->Cookies[] = $cookie;
			$this->pnlShowPollChoices->Visible	= false;
			$this->pnlShowPollChoices->Enabled	= false;
			$this->pnlPollResult->Visible		= true;
			$this->pnlPollResult->Enabled		= true;

			$this->DrawResult();
		}
	}

	public function DrawResult()
	{
		$this->pnlPollResult->Visible	= true;

		$answers	= $this->DataAccess->createCommand("select * from tblpollchoices where pollid='" . $this->Poll->id . "' order by displayorder")->query();
		$totalvotes	= $this->DataAccess->createCommand("select sum(votes) from tblpollchoices where pollid='" . $this->Poll->id . "' order by displayorder")->queryScalar();
		$perc		= 0;
		$resarray	= array();
		$color		= $this->Poll->barcolor;

		foreach($answers as $answer)
		{
			$perc = @round(100 / ($totalvotes / $answer['votes']));
			$barstyle = "width:" . $perc ."px; color: #a0a0a0; background-color: $color;";
			$resarray[]	= array("id" => $answer['id'], "answer" => $answer['answer'], "totalv" => $totalvotes, "perc" => $perc, "votes" => $answer['votes'], "barstyle" => $barstyle);
		}
		$this->rptPollresult->DataSource	= $resarray;
		$this->rptPollresult->dataBind();
	}
}
?>